#!/usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

_RUN_TESTS_SHORT_OPTIONS="
h i x s v f:"

_RUN_TESTS_LONG_OPTIONS="
help with-db-init with-xunit nocapture verbose xunit-file:
"

# Note on OSX bash has no associative arrays (Bash 3.2) so we have to fake it

_RUN_TESTS_KNOWN_VALUES=""

function _get_known_values_run_tests {
    case "$1" in
        *)
            _RUN_TESTS_KNOWN_VALUES=""
    esac
}

_RUN_TESTS_GETOPT_SHORT_OPTIONS=""
_RUN_TESTS_GETOPT_LONG_OPTIONS=""

function _build_options_run_tests {
    local SEPARATOR=""
    local OPTION

    for OPTION in ${_RUN_TESTS_SHORT_OPTIONS}
    do
        _RUN_TESTS_GETOPT_SHORT_OPTIONS="${_RUN_TESTS_GETOPT_SHORT_OPTIONS}${SEPARATOR}${OPTION}"
        SEPARATOR=","
    done

    SEPARATOR=""
    for OPTION in ${_RUN_TESTS_LONG_OPTIONS}
    do
        _RUN_TESTS_GETOPT_LONG_OPTIONS="${_RUN_TESTS_GETOPT_LONG_OPTIONS}${SEPARATOR}${OPTION}"
        SEPARATOR=","
    done
}

function _listcontains_run_tests {
  local WORD
  for WORD in $1; do
    [[ ${WORD} = "$2" ]] && return 0
  done
  return 1
}

# A completion function for run_tests
function _comp_run_tests {
    local ALL_OPTIONS=""
    local EXTRA_ARG_OPTIONS=""
    local OPTION
    local GETOPT_OPTION
    local LAST_COMMAND_PREFIX
    local PREVIOUS_COMMAND
    local ALL_TESTS_FILE="${HOME}/all_tests.txt"

    for OPTION in ${_RUN_TESTS_SHORT_OPTIONS}
    do
        LAST_CHAR="${OPTION:$((${#OPTION}-1)):1}"
        GETOPT_OPTION='-'${OPTION//:/}
        if [[ "${LAST_CHAR}" == ":" ]]; then
            EXTRA_ARG_OPTIONS="${EXTRA_ARG_OPTIONS} ${GETOPT_OPTION}"
        fi
        ALL_OPTIONS="${ALL_OPTIONS} ${GETOPT_OPTION}"
    done
    for OPTION in ${_RUN_TESTS_LONG_OPTIONS}
    do
        LAST_CHAR="${OPTION:$((${#OPTION}-1)):1}"
        GETOPT_OPTION='--'${OPTION//:/}
        ALL_OPTIONS="${ALL_OPTIONS} ${GETOPT_OPTION}"
        if [[ "${LAST_CHAR}" == ":" ]]; then
            EXTRA_ARG_OPTIONS="${EXTRA_ARG_OPTIONS} ${GETOPT_OPTION}"
        fi
    done

    LAST_COMMAND_PREFIX="${COMP_WORDS[${#COMP_WORDS[@]}-1]}"
    if [[ ${#COMP_WORDS[@]} -gt 1 ]]; then
        PREVIOUS_COMMAND="${COMP_WORDS[${#COMP_WORDS[@]}-2]}"
    else
        PREVIOUS_COMMAND=""
    fi

    if _listcontains_run_tests "${EXTRA_ARG_OPTIONS}" "${PREVIOUS_COMMAND}"; then
        COMPREPLY=()
        _get_known_values_run_tests "${PREVIOUS_COMMAND}"
        while IFS='' read -r LINE; do COMPREPLY+=("$LINE"); done \
           < <(compgen -W "${_RUN_TESTS_KNOWN_VALUES}" -- "${LAST_COMMAND_PREFIX}")
    else
        COMPREPLY=()
        while IFS='' read -r LINE; do COMPREPLY+=("$LINE"); done \
           < <(compgen -W "${ALL_OPTIONS}" -- "${LAST_COMMAND_PREFIX}")

        if [[ -f ${ALL_TESTS_FILE} ]]; then
            while IFS='' read -r LINE; do COMPREPLY+=("$LINE"); done \
                < <(compgen -W "$(cat "${ALL_TESTS_FILE}")" -- "${LAST_COMMAND_PREFIX}")
        fi
    fi
}

_build_options_run_tests

# allow completion to contain colon (see http://tiswww.case.edu/php/chet/bash/FAQ)
COMP_WORDBREAKS=${COMP_WORDBREAKS//:}

complete -F _comp_run_tests run-tests
